package com.ying.excel.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.ying.excel.domain.po.ExecelUser;
import com.ying.excel.domain.sheet.CitySheet;
import com.ying.excel.domain.sheet.CompanySheet;
import com.ying.excel.domain.sheet.Mock;
import com.ying.excel.domain.sheet.UserSheet;
import com.ying.excel.service.IExecelUserService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @author jie
 * @since 2024-10-09
 */
@RestController
@RequestMapping("/execel-user")
public class ExecelUserController {

    @Autowired
    private IExecelUserService execelUserService;
    /**
     * 导出数据报表
     */
    @GetMapping("/export")
    @Operation(summary = "导出运营数据报表")
    public void export(HttpServletResponse response){
        execelUserService.exportBusinessData(response);
    }

    /**
     *
     * 在Excel中写入1个sheet
     */
    @RequestMapping("/test")
    public void test1(HttpServletResponse response) {
        //从HttpServletResponse中获取OutputStream输出流
        try {
            // 设置响应类型
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            // 设置编码格式
            response.setCharacterEncoding("utf-8");
            // 设置URLEncoder.encode 防止中文乱码
            String fileName = URLEncoder.encode("用户信息表", "UTF-8").replaceAll("\\+", "%20");
            // 设置响应头
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 写出Excel
            EasyExcel.write(response.getOutputStream(), ExecelUser.class).inMemory(true).sheet("用户信息表").doWrite(execelUserService.list());
        } catch (IOException e) {
            throw new RuntimeException("数据或文件损坏，无法下载");
        }
    }


    /**
     * 在Excel中写入多个sheet
     *
     */
    @RequestMapping("/test2")
    public void test2(HttpServletResponse response) throws Exception {
        // 设置响应类型
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        // 设置编码格式
        response.setCharacterEncoding("utf-8");
        // 设置URLEncoder.encode 防止中文乱码
        String fileName = URLEncoder.encode("信息表", "UTF-8").replaceAll("\\+", "%20");
        // 设置响应头
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        // 多个sheet的输出需要使用ExcelWriter类，这里想要下载成功，需要输出到OutputStream中
        try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).inMemory(true).build()) {
            // 创建用户信息表的sheet，写入用户信息数据，1代表sheet的位置是第一个
            WriteSheet userInfoSheet = EasyExcel.writerSheet(0, "用户信息表").head(UserSheet.class).build();
            excelWriter.write(Mock.userList(), userInfoSheet);
            // 创建城市信息表的sheet，写入城市信息数据，2代表sheet的位置是第二个
            WriteSheet cityInfoSheet = EasyExcel.writerSheet(1, "城市信息表").head(CitySheet.class).build();
            excelWriter.write(Mock.cityList(), cityInfoSheet);
            // 创建公司信息表的sheet，写入公司信息数据，3代表sheet的位置是第三个
            WriteSheet companyInfoSheet = EasyExcel.writerSheet(2, "公司信息表").head(CompanySheet.class).build();
            excelWriter.write(Mock.companyList(), companyInfoSheet);
        }
    }


}
